package com.foreveredu.propms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.BuildingType;
import com.foreveredu.propms.service.IManagementService;
import com.foreveredu.propms.web.struts.actionform.BuildingTypeActionForm;

public class BuildingTypeAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到收费项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<BuildingType> BuildingTypeList = managementService.findBuildingTypes();
		request.setAttribute("buildingTypeList", BuildingTypeList);

		return mapping.findForward("buildingTypeList");
	}

	// 完成收费项目添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingTypeActionForm btf = (BuildingTypeActionForm) form;
		BuildingType BuildingType = new BuildingType();
		PropertyUtils.copyProperties(BuildingType, btf);
		if (btf.getName() == null || "".equals(btf.getName())) {
			request.setAttribute("buildingType", BuildingType);
			request.setAttribute("error", "楼栋类型不能为空！");
			return mapping.findForward("toBuildingTypeList");
		}
		managementService.saveBuildingType(BuildingType);
		return mapping.findForward("toBuildingTypeList");
	}

	// 跳转到收费项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingTypeActionForm btf = (BuildingTypeActionForm) form;
		BuildingType BuildingType = managementService.findBuildingTypeById(btf.getId());
		request.setAttribute("buildingType", BuildingType);

		return mapping.findForward("buildingTypeUpdate");
	}

	// 保存收费项目修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		BuildingTypeActionForm btf = (BuildingTypeActionForm) form;
		BuildingType BuildingType = managementService.findBuildingTypeById(btf.getId());
		BuildingType.setName(btf.getName());
		if(btf.getName()==null || "".equals(btf.getName()) ){
			request.setAttribute("buildingType", BuildingType);
			request.setAttribute("error","楼栋类型不能为空！");
			return mapping.findForward("buildingTypeUpdate");
		}
		managementService.updateBuildingType(BuildingType);
		return mapping.findForward("update");
	}

	// 完成收费项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BuildingTypeActionForm btf = (BuildingTypeActionForm) form;
		managementService.deleteBuildingTypeById(btf.getId());
		return mapping.findForward("delete");
	}

}
